/**
 * 
 */
package com.axxc.cloud.controller.axxc;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.axxc.cloud.pojo.User;
import com.axxc.cloud.pojo.UserExample;
import com.axxc.cloud.pojo.UserExample.Criteria;
import com.axxc.cloud.service.IUserGeneralService;
import com.axxc.cloud.service.IUserService;
import com.axxc.cloud.vo.ResponseVO;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @ClassName: UserController
 * @Description: 用户操作相关接口API
 * @author 熊小川
 * @date 2018年5月4日 下午10:30:13
 */
@RequestMapping("/user")
@RestController
@Api(description = "用户接口文档")
public class UserGeneralController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private IUserService userService;

	@Autowired
	private IUserGeneralService userGeneralService;

	@ApiOperation(value = "增加用户", notes = "增加用户")
	@PostMapping("/addUser")
	public ResponseVO addUser(@RequestBody User record) {

		logger.info(record.toString());
		userService.insertSelective(record);
		
		return new ResponseVO(HttpStatus.OK, record, "success");
	}

	@ApiOperation(value = "根据Id删除用户", notes = "根据Id删除用户") 
	@GetMapping("deleteUserById")
	public ResponseVO deleteUserById(
			@ApiParam(value = "用户ID", required = true) @RequestParam(value = "id") Integer id) {
		int key = userService.deleteByPrimaryKey(id);
		ResponseVO responseVO = new ResponseVO(HttpStatus.OK, false, "success");
		if (key >= 1) {
			responseVO = new ResponseVO(HttpStatus.OK, true, "success");
		}
		return responseVO;

	}

	@ApiOperation(value = "修改用户", notes = "修改用户")
	@PostMapping("/updateUser")
	public ResponseVO updateUser(@ModelAttribute User record,
			@ApiParam(value = "需要修改的用户id", required = true) @RequestParam Integer id) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();

		criteria.andIdEqualTo(id);
		int key = userService.updateByExampleSelective(record, userExample);
		ResponseVO responseVO = new ResponseVO(HttpStatus.OK, false, "failed");
		if (key >= 1) {
			responseVO = new ResponseVO(HttpStatus.OK, true, "success");
		}
		return responseVO;
	}

	@ApiOperation(value = "根据ID查询用户,是否存在", notes = "根据ID查询用户,是否存在")
	@GetMapping("findUserById")
	public ResponseVO findUserById(@ApiParam(value = "用户ID", required = true) @RequestParam(value = "id") Integer id) {
		logger.info(String.valueOf(id));
		User exitsUser = userService.selectByPrimaryKey(id);
		ResponseVO responseVO = new ResponseVO(HttpStatus.OK, true, "success");

		if (exitsUser == null) {
			responseVO.setContent(false);
			responseVO.setMessage("no data");
			responseVO.setStatus(HttpStatus.NO_CONTENT);
		}
		return responseVO;

	}

	@ApiOperation(value = "测试通用mapper接口,查询所有用户信息", notes = "测试通用mapper接口,查询所有用户信息")
	@GetMapping("listAllUser")
	public ResponseVO listAllUser(
			@ApiParam(value = "分页参数合理化,此参数代表页码,从第几页开始,默认1为第一页") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
			@ApiParam(value = "分页参数合理化,此参数每页条数,默认每页5条,") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

		Page<User> page = userGeneralService.listAllUser(pageNum, pageSize);

		logger.info("全部用户信息:{}",page.getResult());

		ResponseVO responseVO;
		if (CollectionUtils.isNotEmpty(page)) {
			responseVO = new ResponseVO(HttpStatus.OK, page, "success");
			responseVO.setTotal(page.getTotal());
		} else {
			responseVO = new ResponseVO(HttpStatus.NO_CONTENT, new ArrayList<User>(), "success");
		}
		return responseVO;

	} 

	@ApiOperation(value = "测试mybatis,parampeterMap String取值", notes = "根据用户名匹配所有满足条件的id")
	@GetMapping("listIdsByUsername")
	public ResponseVO listIdsByUsername(
			@ApiParam(value = "用户名称") @RequestParam(value = "username", defaultValue = "熊大") String username) {
		logger.info("用户名称:{}" + username);
		List<Integer> ids = userService.listIdsByUsername(username);
		
		return new ResponseVO(HttpStatus.OK, ids, "success");

	}

}
